extends MoveElement


class_name FlipElement
"""
根据朝向进行图片翻转
"""


## 是否垂直翻转
@export var is_flip_v: bool = true
## 是否水平翻转
@export var is_flip_h: bool = true
## 适配的对象,默认为MoveComponent适配的对象
@export var wrapper: Node2D = null


var sign_x: int = 1
var sign_y: int = 1


func on_move_ready() -> void:
    if wrapper == null:
        wrapper = move_component.wrapper
    sign_x = sign(wrapper.scale.x)
    sign_y = sign(wrapper.scale.y)

func flip_wrapper() -> void:
    if is_flip_h and not is_zero_approx(move_component.direction.x):
        var s_x: int = sign(move_component.direction.x)
        if s_x != sign_x:
            wrapper.scale.x *= -1
            sign_x = s_x
    if is_flip_v and not is_zero_approx(move_component.direction.y):
        var s_y: int = sign(move_component.direction.y)
        if s_y != sign_y:
            wrapper.scale.y *= -1
            sign_y = s_y

func on_move_direction_changed() -> void:
    flip_wrapper()

func yield_move(delta: float) -> Vector2:
    if move_component.is_moving():
        flip_wrapper()
    return Vector2.ZERO
